/* QUICK CHAT DEMO */
require('/view');
// Show the chat form and bind to the submit action
$.chat = function(key) {
  var msgbox = new $.view(key, 'chat')

  msgbox.checkElement = function(msg) {
    return msg && msg.message && msg.message.length > 0;
  };
  msgbox.beforeAddElement = function (element) {
    console.log('beforeAddElement default')    
    element.hide()
  }
  msgbox.afterAddElement = function (element) {
    console.log('afterAddElement default')    
    element.slideDown()
  }

  msgbox.on('submit', function() {

    // Grab the message from the text box
    var msg = {
      message: msgbox.find('input').first().val()
    }
    
    // Call the 'send' funtion (below) to ensure it's valid before sending to the server
    return msgbox.addElement(msg, function(success) {
      if (success) {
        return msgbox.find('input').first().val('');
      } else {
        return alert('Oops! Unable to send message');
      }
    });
  });

  return msgbox
}

// Private functions

jQuery( "#draggable" ).draggable();

